#include "stdafx.h"  
//#define BOOST_TEST_MODULE ASTAR_TEST
#include <boost/test/unit_test.hpp> 
#include "test_context.h"
#include "test_case_context.h"

using namespace boost::unit_test;
using namespace test;
using namespace graph::algorithm;

graph::global_data_t graph::global::global_;
boost::shared_ptr<test_context_t> test_context;

//____________________________________________________________________________//
void one_dim_astar_test(test_case_context_t& context)
{   
    context.run();
    //2 fails of 5 allowed
    if (!context.get_vertex_not_found())    
    for(int test_count = 0; test_count < 1; ++ test_count)
    {
      BOOST_TEST_CHECKPOINT( "Calling one_dim_astar the " << test_count << " time" );
      BOOST_TEST_CHECKPOINT( "Vertex ids are: "
                              << context.get_start_finish_ids().first << " "
                              << context.get_start_finish_ids().second);
                              
      astar_initializer_t initializer(*global::get_instance().get_current_view_for_graph(), 
                                       context.get_start_finish_ids().first, 
                                       context.get_start_finish_ids().second);
      
      astar_search_t<release, undirectional> astar(global::get_instance().get_current_view_for_graph(), initializer);
            
      astar.run(true, 0);      
      std::list<lat_lon_t> sh_path;
      if (astar.is_all_ended())
         astar.fill_shpath(sh_path);      
      BOOST_CHECK( context.paths_equals(sh_path) );
    }
    else 
      BOOST_CHECK( true );
}

//____________________________________________________________________________//

//____________________________________________________________________________//

void two_dim_astar_test(test_case_context_t& context)
{
   context.run();
   //2 fails of 5 allowed
   if (!context.get_vertex_not_found()) 
   for(int test_count = 0; test_count < 1; ++ test_count)
   {
      BOOST_TEST_CHECKPOINT( "Calling two_dim_astar the " << test_count << " time" );
      BOOST_TEST_CHECKPOINT( "Vertex ids are: "
                              << context.get_start_finish_ids().first << " "
                              << context.get_start_finish_ids().second);
      
      astar_initializer_t initializer(*global::get_instance().get_current_view_for_graph(), 
                                       context.get_start_finish_ids().first, 
                                       context.get_start_finish_ids().second);
      
      astar_search_t <release, bidirectional> astar(global::get_instance().get_current_view_for_graph(), initializer);
      
      astar.run(true, 0);      
      std::list<lat_lon_t> sh_path;
      if (astar.is_all_ended())
         astar.fill_shpath(sh_path);      
      BOOST_CHECK( context.paths_equals(sh_path) );
   }
   else 
      BOOST_CHECK( true );
}
//ids_ = (0x00064b87,0x00062ff7)
//____________________________INIT TEST SUITE SECTION______________________________________//

test_suite* init_unit_test_suite( int argc, char* argv[] ) 
{
   if (argc < 2)
   {
      std::cerr << "Please supply file name with graph as first argument" << std::endl;
      std::cerr << "and count of shortest path search runs as second argument" << std::endl;
      exit(-1);
   }   
   framework::master_test_suite().p_name.value = "astar test suite";
   test_context = boost::shared_ptr<test_context_t>(new test_context_t(argv[1]));
   boost::unit_test::unit_test_log.set_stream( test_context->get_test_log() );
   boost::unit_test::unit_test_log.set_threshold_level( boost::unit_test::log_messages );
   size_t test_count = boost::lexical_cast<size_t>(argv[2]);
   srand(time(0));
   for(size_t i = 0; i < test_count; ++i)
   {
      //lat - from 50 to 70
      //lon - from 20 to 40
      //graph::point_pair_t points(
      //   graph::point_t(55 + ((lat_lon_t)rand() / RAND_MAX) * 10, 25 + ((lat_lon_t)rand() / RAND_MAX) * 10),
      //   graph::point_t(55 + ((lat_lon_t)rand() / RAND_MAX) * 10, 25 + ((lat_lon_t)rand() / RAND_MAX) * 10)
      //);
      vertex_id_pair_t ids(rand() % 100000, rand() % 100000);
      //one dim star test
      framework::master_test_suite().
            add( BOOST_TEST_CASE( boost::bind(&one_dim_astar_test, test_case_context_t(ids)) ) ); //, 2 //2 fails of 5 allowed
      //two dim astar test
      framework::master_test_suite().
            add( BOOST_TEST_CASE( boost::bind(&two_dim_astar_test, test_case_context_t(ids)) )); //,2 //2 fails of 5 allowed 
   } 

   return 0;
}

//____________________________________________________________________________//
